<template>
  <div class="cart-item-card" v-if="data?.id">
    <van-card 
      class="product-card"
      @click.stop="handleCardClick(data.shopper)"
      :price="data?.price"
      :desc="data?.shopper"
      :title="data?.title"
      :thumb="data?.img"
    >
      <template #tags>
        <div class="product-tags" v-if="data?.label?.length">
          <van-tag 
            v-for="(tag, index) in data.label" 
            :key="index" 
            plain 
            type="primary"
            size="small"
            class="product-tag"
          >
            {{ tag }}
          </van-tag>
        </div>
      </template>
      
      <template #footer>
        <div class="card-footer">
          <div class="quantity-control" v-if="showQuantity" @click.stop>
            <!-- 步进器计数 -->
            <van-stepper 
              v-model="itemNum"
              :min="1"
              :max="99"
              @change="handleQuantityChange"
              size="22"
            />
          </div>
          
          <div class="confirm-section" @click.stop>
            <van-checkbox 
              v-model="isChecked"
              checked-color="#ee0a24"
              @change="handleConfirmChange"
              class="confirm-checkbox"
            >
              <span class="confirm-text">确认</span>
            </van-checkbox>
          </div>
        </div>
      </template>
    </van-card>
  </div>
  <div v-else class="cart-item-error"></div>
</template>

<script setup>
import { ref,watch } from 'vue'
import { useRouter } from 'vue-router'
import { useShopperStore } from '../store/shopper'
const shopperStore = useShopperStore()
const router = useRouter()
const props = defineProps({
  data: {
    type: Object,
    required: true
  },
  showQuantity: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['confirm-change', 'quantity-change'])

// 状态管理
const isChecked = ref(false)
const itemNum = ref(1)

// 监听父组件(cart.vue)数据变化 实现响应式的购物车  当数据发生变化时 同步商品的状态  确认 以及数量
watch(() => props.data, (newData) => {
  if (newData) {
    isChecked.value = Boolean(newData.isConfirmed)
    itemNum.value = newData.num || 1
  }
}, { immediate: true, deep: true })

// 事件处理  发送修改确认状态事件  父组件接收到再在cartList中修改商品状态
const handleConfirmChange = (value) => {
  emit('confirm-change', {
    item: props.data,
    confirmed: value
  })
}
// 事件处理  发送修改数量事件  父组件接收到再在cartList中修改商品数量
const handleQuantityChange = (value) => {
  emit('quantity-change', {
    item: props.data,
    quantity: value
  })
}

const handleCardClick = (shopper) => {
    // 点击卡片跳转到商家详情页
    shopperStore.shopper=shopper
    router.push({
      path: '/shopper',
    })
    console.log('点击商品卡片:', props.data?.title)
  
}
</script>

<style scoped lang="scss">
.cart-item-card {
  margin-bottom: 12px;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .product-card {
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    
    &:hover {
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
      transform: translateY(-2px);
    }
    
    &:deep(.van-card__header) {
      padding: 12px;
    }
    
    &:deep(.van-card__content) {
      min-height: 80px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }
    
    &:deep(.van-card__title) {
      font-size: 16px;
      font-weight: 500;
      color: #333;
      margin-bottom: 4px;
      line-height: 1.4;
    }
    
    &:deep(.van-card__desc) {
      font-size: 13px;
      color: #666;
      margin-bottom: 8px;
    }
    
    &:deep(.van-card__price) {
      font-size: 18px;
      font-weight: 600;
      color: #ee0a24;
    }
    
    &:deep(.van-card__footer) {
      padding: 8px 12px;
      background: #fafafa;
      border-top: 1px solid #f0f0f0;
    }
  }
}

.product-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 8px;
  
  .product-tag {
    font-size: 10px;
    padding: 2px 6px;
  }
}



.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  .quantity-control {
    flex: 1;
    
    &:deep(.van-stepper) {
      height: 28px;
      
      .van-stepper__minus,
      .van-stepper__plus {
        width: 28px;
        height: 28px;
        background: #f5f5f5;
        border-radius: 4px;
        border: none;
      }
      
      .van-stepper__input {
        height: 28px;
        background: #fff;
        border-radius: 4px;
        margin: 0 4px;
        font-size: 14px;
        font-weight: 500;
      }
    }
  }
  
  .confirm-section {
    flex-shrink: 0;
    
    .confirm-checkbox {
      &:deep(.van-checkbox__icon) {
        font-size: 18px;
      }
      
      &:deep(.van-checkbox__label) {
        margin-left: 6px;
      }
    }
    
    .confirm-text {
      font-size: 14px;
      color: #666;
      font-weight: 500;
    }
  }
}

.cart-item-error {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border-radius: 8px;
  margin-bottom: 12px;
}

// 响应式设计
@media (max-width: 768px) {
  .cart-item-card {
    .product-card {
      &:deep(.van-card__title) {
        font-size: 15px;
      }
      
      &:deep(.van-card__price) {
        font-size: 16px;
      }
    }
    
    .card-footer {
      .quantity-control {
        &:deep(.van-stepper) {
          height: 26px;
          
          .van-stepper__minus,
          .van-stepper__plus {
            width: 26px;
            height: 26px;
          }
          
          .van-stepper__input {
            height: 26px;
            font-size: 13px;
          }
        }
      }
    }
  }
}
</style>